package com.zf.yichat.utils.HJpay.com.sdk.utils;


import com.zf.yichat.utils.HJpay.com.sdk.constants.Algorithm;
import com.zf.yichat.utils.HJpay.com.sdk.constants.CommonConst;
import com.zf.yichat.utils.HJpay.com.sdk.exceptions.JPException;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5工具类
 * @author chenyf
 * @date 2018-12-15
 */
public class MD5Util {

    /**
     * 生成16进制的MD5字符串
     * @param str
     * @return
     */
    public static String getMD5Hex(String str) {
        return HEXUtil.encode(getMD5(str), true);
    }

    public static byte[] getMD5(String str) {
        MessageDigest messageDigest;
        try {
            messageDigest = MessageDigest.getInstance(Algorithm.MD5);
            messageDigest.reset();
            if (StringUtil.isNotEmpty(str)) {
                messageDigest.update(str.getBytes(CommonConst.ENCODING_UTF_8));
            }
        } catch (NoSuchAlgorithmException e) {
            throw new JPException("生成MD5信息时异常", e);
        } catch (UnsupportedEncodingException e) {
            throw new JPException("生成MD5信息时异常", e);
        }

        return messageDigest.digest();
    }
}
